<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <h2>数组更新检测</h2>
      <p v-for="(item, index) in list" :key="index">{{ item }}</p>
      <button @click="fn">btn</button>

      <hr />

      <p>{{ list2 }}</p>
      <!-- <p>{{ obj }}</p> -->
    </div>

    <script src="../Vue.js"></script>
    <script>
      new Vue({
        el: "#app",
        data: {
          list: [1, 2, 3, 4, 5, 6, 7],
          list2: [1, 2, 3, 4],
          // obj: {
          //   a: 3,
          // },
        },
        methods: {
          fn() {
            // this.list.push(9);
            // this.list = this.list.concat(9);

            // 会使vue的响应式无效
            // 直接改变数组长度
            this.list2.length = 2;
            this.list2 = [...this.list2];
            // this.list2 = [].concat(this.list2);
            // console.log(this.list2);

            // 通过数组索引改变数组项
            // this.list2[1] = 20;
            // console.log(this.list2);

            // 在对象中添加新属性
            // this.obj.b = 4;
            // this.obj = Object.assign({}, this.obj);
            // this.obj = { ...this.obj };
          },
        },
      });
    </script>
  </body>
</html>
